import Dialog from "../../../../component/vant/dialog/dialog";
const checkUtil = require('../../../../utils/checkUtil')

let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        btnDisable: false,
        spinShow: false,
        tableData: [],
        id: null,
        account: null,
        name: null,
        phone: null,
        password: null,
        roleIdList: [],
        // list: [{
        //     "id": 1,
        // }, {
        //     "id": 2,
        // }, {
        //     "id": 3,
        // }],
        // result: [1, "2"],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let id = options.id
        if (id) {
            this.setData({
                id: id
            })
            this.queryAccountData()
        }
        this.queryData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    onChangeRole(event) {
        this.setData({
            roleIdList: event.detail,
        });
    },
    toggle(event) {
        const {index} = event.currentTarget.dataset;
        const checkbox = this.selectComponent(`.checkboxes-${index}`);
        checkbox.toggle();
    },

    noop() {
    },
    queryAccountData: function () {
        let _this = this
        app.formGet('/app/boss/detail?id=' + this.data.id, null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    account: res.data.account,
                    name: res.data.name,
                    phone: res.data.phone,
                    roleIdList: res.data.roleIdList
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/role/list', null).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                _this.setData({
                    tableData: res.data.list
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    // 角色保存
    handleSaveAccount() {
        let _this = this
        if (checkUtil.checkNull(this.data.name)) {
            app.toast("请输入员工名称", "fail")
            return false;
        }
        if (!checkUtil.checkMobile(this.data.phone)) {
            app.toast("手机号格式不正确", "fail")
            return false;
        }
        _this.setData({
            btnDisable: true
        })
        let data = {}
        data.id = this.data.id
        data.account = this.data.account
        data.name = this.data.name
        data.phone = this.data.phone
        data.password = this.data.password
        data.roleIdList = this.data.roleIdList
        let url = "/app/boss/add"
        if (this.data.id != null) {
            url = "/app/boss/edit"
        }
        app.formPost(url, data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 2000)
            } else {
                app.message(res.message, 'error')
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false,
                btnDisable: false
            });
            app.message(e, 'error')
        })
    },
    handleDelAccount() {
        let _this = this
        Dialog.confirm({
            title: '删除提醒',
            message: '是否确认此员工账号',
        })
            .then(() => {
                _this.setData({
                    btnDisable: true
                })
                let data = {}
                data.id = this.data.id
                app.formPost('/app/boss/del', data).then(res => {
                    _this.setData({
                        spinShow: false
                    });
                    if (res.code === 0) {
                        app.message(res.message, 'success')
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 2000)
                    } else {
                        app.message(res.message, 'error')
                    }
                    _this.setData({
                        btnDisable: false
                    })
                }).catch(e => {
                    _this.setData({
                        spinShow: false,
                        btnDisable: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                console.log("concel")
                // on cancel
            });
    }
})